package com.czy.owner.ui.coupons;

import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;

import com.czy.owner.R;
import com.czy.owner.adapter.MyCouPonsAdapter;
import com.czy.owner.api.OwnCouponsListApi;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.CouponsCountModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseFragment;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;


/**
 * Created by wangyang on 2017/4/13 0013.
 * 过期的
 */

public class OverduFragment extends BaseFragment implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.easy_recycleview)
    EasyRecyclerView recyclerView;
    private MyCouPonsAdapter adapter;
    private int page = 1;
    private Handler handler = new Handler();

    @Override
    protected int attachLayoutRes() {
        return R.layout.common_fragment_easyrecycleview;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapterWithProgress(adapter = new MyCouPonsAdapter(getActivity(), "已过期"));
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore, new RecyclerArrayAdapter.OnNoMoreListener() {
            @Override
            public void onNoMoreShow() {
                //adapter.resumeMore();
            }

            @Override
            public void onNoMoreClick() {
                adapter.resumeMore();
            }
        });

        adapter.setError(R.layout.view_error_net, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();

            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();

            }
        });
        recyclerView.setRefreshListener(this);
    }

    @Override
    protected void updateViews() {
        onRefresh();
        MyLog.e("yang", "已过期");
    }


    @Override
    public void onLoadMore() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新
                if (!PhoneUtils.isNetworkConnected(getActivity())) {
                    adapter.pauseMore();
                    recyclerView.showError();
                    return;
                }
                page++;
                getDataList();

            }
        }, 1000);
    }

    @Override
    public void onRefresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新
                adapter.clear();
                page = 1;
                getDataList();
            }
        }, 1000);
    }

    private void getDataList() {

        OwnCouponsListApi ownCouponsListApi = new OwnCouponsListApi(new HttpOnNextListener<CouponsCountModel>() {
            @Override
            public void onNext(CouponsCountModel model) {
                setDataList(model.getList());
            }

            @Override
            public void onCacheNext(String cache) {
                super.onCacheNext(cache);
                Gson gson = new Gson();
                Type type = new TypeToken<BaseResultEntity<CouponsCountModel>>() {
                }.getType();
                BaseResultEntity<CouponsCountModel> cacheList = gson.fromJson(cache, type);
                setDataList(cacheList.getData().getList());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                PhoneUtils.ShowToastMessage(getActivity(), e.getMessage());
            }
        }, (RxAppCompatActivity) getContext(), page + "", "hasExpired");

        ownCouponsListApi.setSession(UserHelper.getInstance().getSessionInfoModel(getActivity()).getSession());
        ownCouponsListApi.setStatus("hasExpired");
        ownCouponsListApi.setPage(page + "");
        ownCouponsListApi.setSize("10");
//        ownCouponsListApi.setStoreId(SharedPreferencesUtils.getParam(getActivity(), UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
        ownCouponsListApi.setCloseMall(UserHelper.getInstance().isCloseMall()+"");
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(ownCouponsListApi);

    }


    private void setDataList(List<CouponsCountModel.ListBean> list) {
        if (list.size() < 9) {
            adapter.stopMore();
        }

        adapter.addAll(list);
        if (adapter.getAllData().size() == 0) {
            recyclerView.setEmptyView(R.layout.view_empty_overdu_coupons);
            recyclerView.showEmpty();
        }
    }

}